/**
 *
 * by littlefean
 */
window.onload = function () {
    let inputExpression = document.querySelector(`.inputExpression`);
    let output = document.querySelector(`.output`);

    inputExpression.onblur = function () {
        let expStr = inputExpression.value;
        let sufStr = InfixToSuffix.run(expStr);
        let list = sufStr.split(" ");
        output.innerHTML = "";
        let stack = [];
        // 一遍遍历后缀表达式，一边进行栈模拟运算
        for (let strItem of list) {
            if (strIsInt(strItem)) {
                // 如果当前这个字符串片段是一个数字
                stack.push(FractionNumber.evalString(strItem));
            } else {
                // 如果是运算符了
                let b = stack.pop();
                let a = stack.pop();
                let res;
                switch (strItem) {
                    case "+":
                        res = a.add(b)
                        output.appendChild(div(`${a.toHtml()}+${b.toHtml()}=${res.toHtml()}`));
                        stack.push(res);
                        break;
                    case "-":
                        b.up *= -1;
                        res = a.add(b);
                        stack.push(res);
                        output.appendChild(div(`${a.toHtml()}+${b.toHtml()}=${res.toHtml()}`));
                        break;
                    case "*":
                        res = a.mul(b);
                        stack.push(res);
                        output.appendChild(div(`${a.toHtml()}·${b.toHtml()}=${res.toHtml()}`));
                        break;
                    case "/":
                        b.reverse();
                        res = a.mul(b)
                        stack.push(res);
                        output.appendChild(div(`${a.toHtml()}·${b.toHtml()}=${res.toHtml()}`));
                        break;
                }
            }
        }
        output.appendChild(div(`最终结果是：${stack[0].toHtml()}`));

    }


}
